package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysContract;
import com.ruoyi.system.domain.SysContractLog;
import com.ruoyi.system.domain.SysTeacherClass;
import com.ruoyi.system.service.ISysContractLogService;
import com.ruoyi.system.service.ISysContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysContractLabelMapper;
import com.ruoyi.system.domain.SysContractLabel;
import com.ruoyi.system.service.ISysContractLabelService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 合同明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-17
 */
@Service
public class SysContractLabelServiceImpl implements ISysContractLabelService
{
    @Autowired
    private SysContractLabelMapper sysContractLabelMapper;

    /** 合同service接口层 */
    @Autowired
    private ISysContractService sysContractService;

    /** 合同日志service接口层 */
    @Autowired
    private ISysContractLogService sysContractLogService;

    /**
     * 查询合同明细
     *
     * @param id 合同明细主键
     * @return 合同明细
     */
    @Override
    public SysContractLabel selectSysContractLabelById(Long id)
    {
        return sysContractLabelMapper.selectSysContractLabelById(id);
    }

    /**
     * 根据合同主键查询合同列表
     * @param contractId
     * @return
     */
    @Override
    public List<SysContractLabel> selectSysContractLabelByContractId(Long contractId) {
        return sysContractLabelMapper.selectSysContractLabelByContractId(contractId);
    }

    /**
     * 查询合同明细列表
     *
     * @param sysContractLabel 合同明细
     * @return 合同明细
     */
    @Override
    public List<SysContractLabel> selectSysContractLabelList(SysContractLabel sysContractLabel)
    {
        return sysContractLabelMapper.selectSysContractLabelList(sysContractLabel);
    }

    /**
     * 根据教师主键查询合同明细
     * @param sysTeacherClass
     * @return
     */
    @Override
    public List<SysContractLabel> selectContractLabelLIstByTeacherId(SysTeacherClass sysTeacherClass) {
        return sysContractLabelMapper.selectContractLabelLIstByTeacherId(sysTeacherClass);
    }

    /**
     * 新增合同明细
     *
     * @param sysContractLabel 合同明细
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysContractLabel(SysContractLabel sysContractLabel)
    {
        int status = 0;
        try {
            //新增合同明细判断方法
            emptyInsertSysContractLabel(sysContractLabel);

            //构建合同明细
            sysContractLabel = buildSysContractLabel(sysContractLabel);

            //修改合同金额
            sysContractService.updateSysContractMoney(sysContractLabel.getContractId(),sysContractLabel.getTotalPrice(),true);

            //修改剩余金额
            sysContractService.updateSysContractRemainingMoney(sysContractLabel.getContractId(),sysContractLabel.getTotalPrice(),true);

            //修改合同剩余时间
            sysContractService.updateSysContractRemainingClassTime(sysContractLabel.getContractId(),sysContractLabel.getSubjectClassTime(),true);

            //新增合同明细
            status = sysContractLabelMapper.insertSysContractLabel(sysContractLabel);

            //构建
            SysContractLog sysContractLog = sysContractLogService.buildSysContractLog(sysContractLabel.getContractId(), "创建合同明细");

            //新增合同日志
            sysContractLogService.insertSysContractLog(sysContractLog);
        }
        catch (Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }
        return status;
    }

    /**
     * 构建合同明细
     * @param sysContractLabel
     * @return
     */
    @Override
    public SysContractLabel buildSysContractLabel(SysContractLabel sysContractLabel) {
        //创建人
        sysContractLabel.setCreateBy(SecurityUtils.getUsername());

        //创建时间
        sysContractLabel.setCreateTime(DateUtils.getNowDate());

        //总价
        sysContractLabel.setTotalPrice(sysContractLabel.getPrice().multiply(new BigDecimal(sysContractLabel.getSubjectClassTime())));

        //剩余课时
        sysContractLabel.setRemainingClassTime(sysContractLabel.getSubjectClassTime());

        //已上课时
        sysContractLabel.setDuringClassTime(0L);

        //已排未上课时
        sysContractLabel.setNotDuringClassTime(0L);

        //状态
        sysContractLabel.setStatus(0L);

        return sysContractLabel;
    }

    /**
     * 新增合同明细判断方法
     * @param sysContractLabel
     */
    @Override
    public void emptyInsertSysContractLabel(SysContractLabel sysContractLabel) {
        //合同表头主键
        if (null == sysContractLabel.getContractId()){
            throw new RuntimeException("合同表头信息未传输，请联系管理员");
        }
        //科目
        if (null == sysContractLabel.getSubjectClassId()){
            throw new RuntimeException("请选择科目");
        }
        //课时
        if (null == sysContractLabel.getSubjectClassTime() || sysContractLabel.getSubjectClassTime() == 0){
            throw new RuntimeException("课时不可为空或者0");
        }
        //明细类型
        if (null == sysContractLabel.getLabelType()){
            throw new RuntimeException("请选择明细类型");
        }
        //如果明细类型为赠送则单价不可大于0
        if (sysContractLabel.getLabelType() == 2){
            if (sysContractLabel.getPrice().compareTo(new BigDecimal(0)) != 0){
                throw new RuntimeException("明细类型为赠送时，单价不可大于0");
            }
        }
        //折扣
        if (null == sysContractLabel.getScholarshipDiscount()){
            throw new RuntimeException("折扣为传输，请联系管理员");
        }
    }

    /**
     * 修改合同明细
     *
     * @param sysContractLabel 合同明细
     * @return 结果
     */
    @Override
    public int updateSysContractLabel(SysContractLabel sysContractLabel)
    {
        return sysContractLabelMapper.updateSysContractLabel(sysContractLabel);
    }

    /**
     * 批量删除合同明细
     *
     * @param ids 需要删除的合同明细主键
     * @return 结果
     */
    @Override
    public int deleteSysContractLabelByIds(Long[] ids)
    {
        return sysContractLabelMapper.deleteSysContractLabelByIds(ids);
    }

    /**
     * 删除合同明细信息
     *
     * @param id 合同明细主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysContractLabelById(Long id)
    {
        int status = 0;
        try {
            //查询合同明细
            SysContractLabel sysContractLabel = this.selectSysContractLabelById(id);
            if (null == sysContractLabel){
                throw new RuntimeException("未查询到合同明细，请联系管理员");
            }

            //查询合同
            sysContractService.selectSysContractById(sysContractLabel.getContractId());

            if (sysContractLabel.getStatus() != 0){
                throw new RuntimeException("当前明细已进入审批流或已审核，不可删除");
            }

            //修改合同剩余金额
            sysContractService.updateSysContractRemainingMoney(sysContractLabel.getContractId(),sysContractLabel.getTotalPrice(),false);

            //修改合同金额
            sysContractService.updateSysContractMoney(sysContractLabel.getContractId(),sysContractLabel.getTotalPrice(),false);

            //修改合同剩余课时
            sysContractService.updateSysContractRemainingClassTime(sysContractLabel.getContractId(),sysContractLabel.getSubjectClassTime(),false);

            //合同明细删除方法
            status = sysContractLabelMapper.deleteSysContractLabelById(id);

            //构建合同日志
            SysContractLog sysContractLog = sysContractLogService.buildSysContractLog(sysContractLabel.getContractId(), "删除合同明细");

            //新建合同日志
            sysContractLogService.insertSysContractLog(sysContractLog);
        }
        catch (Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }
        return status;
    }

    /**
     * 根据合同主键删除合同明细
     * @param contractId
     * @return
     */
    @Override
    public int deleteSysContractLabelByContractId(Long contractId) {
        return sysContractLabelMapper.deleteSysContractLabelByContractId(contractId);
    }
}